﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace dev173d
{
    class Player
    {

        //KeyboardState ks, old_ks;
        //MouseState mouse, mouseanterior;
        Vector2 mouseposition;
        Vector2 mousepositionanterior;

        GameWindow window;

        Model scarl;

        BasicCamera camera;
        
        bool abaixado = false;
        bool correndo = false;

        Weapon arma;

        public Player(GameWindow window,BasicCamera camera,Game game)
        {
            scarl = game.Content.Load<Model>("scarl");
            arma = new Weapon(game, scarl, camera);


            this.camera = camera;

            this.window = window;

        }


        public void Update(GameTime gameTime, KeyboardState keyboar,KeyboardState ks,MouseState mouse, MouseState mouseanterior)
        {
            

            #region Movimento do Personagem

            #region Mira


            if (mouse.RightButton == ButtonState.Pressed && mouseanterior.RightButton == ButtonState.Released)
            {
                camera.Position += new Vector3(0, 0, -5);
                camera.Target += new Vector3(0, 0, -5);
                arma.WeaponPositionZ = 2f;

            }
            if (mouse.RightButton == ButtonState.Released && mouseanterior.RightButton == ButtonState.Pressed)
            {
                camera.Position -= new Vector3(0, 0, -5);
                camera.Target -= new Vector3(0, 0, -5);


                arma.WeaponPositionZ = 3.2f;
            }



            // Criando a rotação do Mouse

            mousepositionanterior = mouseposition;

            mouseposition = new Vector2(mouse.X, mouse.Y);


            int centerX = window.ClientBounds.Width / 2;
            int centerY = window.ClientBounds.Height / 2;
            int deltaX = centerX - mouse.X;
            int deltaY = centerY - mouse.Y;

            Mouse.SetPosition(centerX, centerY);

            camera.LookMove(deltaX, deltaY);


            ////


            #endregion


            if (ks.IsKeyDown(Keys.LeftControl) && abaixado == false)
            {
                abaixado = true;
                //camera.Position += new Vector3(0.3f, 0, 0);
                camera.Position -= new Vector3(0, 3.5f, 0);
            }
            else if (ks.IsKeyUp(Keys.LeftControl) && abaixado == true)
            {
                abaixado = false;
                camera.Position += new Vector3(0, 3.5f, 0);
            }

            if (ks.IsKeyDown(Keys.LeftShift) && correndo == false && abaixado == false)
            {
                correndo = true;
            }

            else if (ks.IsKeyUp(Keys.LeftShift) && correndo == true && abaixado == false)
            {
                correndo = false;
            }




            if (ks.IsKeyDown(Keys.D))
            {
                Vector3 deslocamento;

                deslocamento = camera.XAxis;

                deslocamento.Normalize();

                if (correndo == false)
                {
                    deslocamento *= new Vector3(0.3f, 0, 0.3f);
                    camera.Position += deslocamento;
                }

                else
                {
                    deslocamento *= new Vector3(0.8f, 0, 0.8f);
                    camera.Position += deslocamento;
                }

            }


            if (ks.IsKeyDown(Keys.A))
            {
                Vector3 deslocamento;

                deslocamento = camera.XAxis;

                deslocamento.Normalize();

                if (correndo == false)
                {
                    deslocamento *= new Vector3(0.3f, 0, 0.3f);
                    camera.Position -= deslocamento;
                }

                else
                {
                    deslocamento *= new Vector3(0.8f, 0, 0.8f);
                    camera.Position -= deslocamento;
                }

            }
            if (ks.IsKeyDown(Keys.S))
            {
                Vector3 deslocamento;

                deslocamento = camera.ZAxis;

                deslocamento.Normalize();


                if (correndo == false)
                {

                    deslocamento *= new Vector3(0.3f, 0, 0.3f);
                    camera.Position += deslocamento;

                }

                else
                {
                    deslocamento *= new Vector3(0.8f, 0, 0.8f);
                    camera.Position += deslocamento;
                }


            }
            if (ks.IsKeyDown(Keys.W))
            {
                Vector3 deslocamento;

                deslocamento = camera.ZAxis;

                deslocamento.Normalize();



                if (correndo == false)
                {

                    deslocamento *= new Vector3(0.3f, 0, 0.3f);
                    camera.Position -= deslocamento;

                }

                else
                {
                    deslocamento *= new Vector3(0.8f, 0, 0.8f);
                    camera.Position -= deslocamento;
                }



            }




            #endregion

            arma.Update(gameTime);
        }


        public void Draw(GameTime gameTime)
        {
            arma.Draw(gameTime);

        }


    }
}
